package com.example.testapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.testapplication.R;
import com.example.testapplication.api.Api;
import com.example.testapplication.api.ApiConfig;
import com.example.testapplication.api.CallBack;
import com.example.testapplication.entity.AddressEntity;
import com.example.testapplication.entity.CartEntity;
import com.example.testapplication.entity.Result;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

public class AddressAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private Context mContext;
    private List<AddressEntity> listDatas;
    public void setListDatas(List<AddressEntity> listDatas) {
        this.listDatas = listDatas;
    }

    public AddressAdapter(Context context) {
        this.mContext = context;
    }
    public AddressAdapter(Context context, List<AddressEntity> datas) {
        this.listDatas=datas;
        this.mContext = context;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_address,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;
        AddressEntity addressEntity = listDatas.get(position);
        vh.tvConsignee.setText(addressEntity.getConsignee());
        vh.tvAddress.setText(addressEntity.toString());
        vh.tvLabel.setText(addressEntity.getLabel());
        if(addressEntity.getIsdefault()==1){
            vh.btnDefault.setEnabled(false);
        }else{
            vh.btnDefault.setEnabled(true);
        }
        vh.btnDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setDefaultAddress(addressEntity.getId());
            }
        });
    }

    private void setDefaultAddress(int id) {
        HashMap<String,Object> params = new HashMap<String,Object>();
        params.put("id", id);
        Api.config(ApiConfig.USER_DEFAULT_ADDRESS,params).putRequest(mContext,new CallBack() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                Result res = gson.fromJson(result, Result.class);
                if(res.getCode() == 1){

                }else{

                }
            }

            @Override
            public void onFailure(IOException e) {

            }
        });
    }

    @Override
    public int getItemCount() {
        if(listDatas!=null&&listDatas.size()>0)
        {
            return listDatas.size();
        }else{
            return 0;
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tvConsignee;
        private TextView tvAddress;
        private TextView tvLabel;
        private Button btnDefault;

        public ViewHolder(@NonNull View view) {
            super(view);
            tvConsignee=view.findViewById(R.id.address_consignee);
            tvAddress=view.findViewById(R.id.address_design);
            tvLabel=view.findViewById(R.id.address_label);
            btnDefault = view.findViewById(R.id.btn_default);
        }
    }
}

